﻿var PowerAjax = function () { }
PowerAjax.__Private = function () { }

// 进行异步操作
PowerAjax.AsyncAjax = function (methodName, paramArray, success) {
    PowerAjax.__Private.Ajax(methodName, paramArray, success, true);
}

// 进行的是同步操作
PowerAjax.SyncAjax = function (methodName, paramArray, success) {
    PowerAjax.__Private.Ajax(methodName, paramArray, success, false);
}

PowerAjax.__Private.Ajax = function (methodName, paramArray, success, isAsync) {
    var data = {};
    switch (paramArray.length) {
        case 0:
            data = { 'isAjaxRequest': true, 'MethodName': methodName };
            break;
        case 1:
            data = { 'isAjaxRequest': true, 'MethodName': methodName, "param0": paramArray[0] };
            break;
        case 2:
            data = { 'isAjaxRequest': true, 'MethodName': methodName, "param0": paramArray[0], "param1": paramArray[1] };
            break;
        case 3:
            data = { 'isAjaxRequest': true, 'MethodName': methodName, "param0": paramArray[0], "param1": paramArray[1], "param2": paramArray[2] };
            break;
        case 4:
            data = { 'isAjaxRequest': true, 'MethodName': methodName, "param0": paramArray[0], "param1": paramArray[1], "param2": paramArray[2], "param3": paramArray[3] };
            break;
        case 5:
            data = { 'isAjaxRequest': true, 'MethodName': methodName, "param0": paramArray[0], "param1": paramArray[1], "param2": paramArray[2], "param3": paramArray[3], "param4": paramArray[4] };
            break;
    }

    var url = document.location.href;
    $.ajax({
        type: "post",
        url: url,
        data: data,
        async: isAsync,
        datatype: "json",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (response) {
            success(response);
        },
        error: function (response) {
            if (response.status == 500) {
                var errorMessage = response.responseText;
                var errorTitle = errorMessage.substring(errorMessage.indexOf("<title>") + 7, errorMessage.indexOf("</title>"))
                throw new Error("服务器内部错误：" + errorTitle);
            }
        }
    });
}